package com.cardbaobao.cardbabyclient.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.cardbaobao.cardbabyclient.callback.ItemClickedCallback;
import com.cardbaobao.cardbabyclient.model.Banner;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.List;

/**
 * Created by Jiangzx on 2017/9/1 17:06.
 */

public class BannerListAdapter extends RecyclerView.Adapter {
    private List<Banner.CarouselFigureListBean> data;
    private ItemClickedCallback<Banner.CarouselFigureListBean> listener;

    private int radian;
    private int topMargin;
    private int leftMargin;
    private int rightMargin;
    private int bottomMargin;

    public BannerListAdapter(List<Banner.CarouselFigureListBean> data){
        this.data=data;
        this.radian= DensityUtil.dp2px(6);
        final int margin=DensityUtil.dp2px(10);
        this.leftMargin=margin;
        this.rightMargin=margin;
        this.topMargin=margin;
        this.bottomMargin=margin;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ImageView imageView=new ImageView(parent.getContext());
        //图片宽高比例 710:280
        final int screenWidth= ScreenUtil.getScreenWidth();
        ViewGroup.MarginLayoutParams params=
                new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.MATCH_PARENT,
                        (int) (screenWidth*28/71.0f));
        params.leftMargin=leftMargin;
        params.topMargin=topMargin;
        params.rightMargin=rightMargin;
        imageView.setLayoutParams(params);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        return new RecyclerView.ViewHolder(imageView) {
        };
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Banner.CarouselFigureListBean bean=data.get(position);
        ImageView iv= (ImageView) holder.itemView;
        ImageLoaderUtil.displayImage(URLUtil.CBB_IMG_URL+ bean.getImage(),
                iv,
                radian);
        if(position==getItemCount()-1){
            try{
                ViewGroup.MarginLayoutParams layoutParams=
                        (ViewGroup.MarginLayoutParams) holder.itemView.getLayoutParams();
                layoutParams.bottomMargin=bottomMargin;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.onItemClicked(bean);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    public void setItemClickedListener(ItemClickedCallback<Banner.CarouselFigureListBean> listener){
        this.listener=listener;
    }
}
